﻿/*
(c) 2010 - Open Source Business Foundation e. V. (www.osbf.de)
This software is provided under Microsoft Reciprocal License (Ms-RL)
(http://www.microsoft.com/opensource/licenses.mspx#Ms-RL)
All other rights reserved
*/
using System.Collections;
using System.Collections.Generic;
using System.Data.Services.Providers;
using DataServiceProvider;

namespace MicroFormatWebRole.OData {

    /// <summary>
    /// Abstract Class. This class gathers the objects which are queryied by the OData call.
    /// </summary>

    public abstract class MfDSPResourceList : IList<DSPResource> {
        protected DSPContext context { get; set;}
        protected DSPMetadata metadata { get; set; }
        private IList<DSPResource> List = null;

        public MfDSPResourceList(DSPContext context, DSPMetadata metadata) : base() {
            this.context = context;
            this.metadata = metadata;
        }

        protected abstract void LoadData() ; 

        protected DSPResource CreateResourceObject(ResourceType resourceType, string[] propertyNames, params object[] values) {
            DSPResource res = new DSPResource(resourceType);

            for (int i = 0; i < propertyNames.Length; ++i) {
                res.SetValue(propertyNames[i], values[i]);
            }

            return res;
        }

        public DSPResource this[int index] {
            get {
                return List[index];
            }
            set {
                List[index] = value;
            }
        }

        public int IndexOf(DSPResource item) {
            return List.IndexOf(item);
        }

        public void Insert(int index, DSPResource item) {
            List.Insert(index, item);
        }

        public void RemoveAt(int index) {
            List.RemoveAt(index);
        }

        public int Count { 
            get {
                return List.Count;
            } 
        }


        public bool IsReadOnly { 
            get {
                return List.IsReadOnly;
            } 
        }

        public void Add(DSPResource item) {
            List.Add(item);
        }

        public void Clear() {
            List.Clear();
        }

        public bool Contains(DSPResource item) {
            return List.Contains(item);
        }

        public void CopyTo(DSPResource[] array, int arrayIndex) {
            List.CopyTo(array, arrayIndex);
        }

        public bool Remove(DSPResource item) {
            return List.Remove(item);
        }

        public IEnumerator<DSPResource> GetEnumerator() {
            if (List == null) {
                List = new List<DSPResource>();
            } else {
                List.Clear();
            }
            this.LoadData();
            return List.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return GetEnumerator();
        }

    }
}
